<?php

/**
 * 商品分析结果
 * File Name：GuestFenxiModel.php
 * File Encoding：UTF-8
 * File New Time：2014-8-22 15:28:24
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GuestFenxiModel extends BaseModel {
    
    public $_pk = 'id';

    public function tableName() {
        return '{{guest_fenxi}}';
    }

    public function rules() {
        return array(
            array('keys,goods_id,product_id,admin_id,cat_id,start_time,end_time,bm_uid,add_time,years,month,day,logtime,view,turnover,guan_turnover,you_turnover,userfanw,youkefanw,sales,savetime', 'safe'),
        );
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->savetime = time();
        } else {
            
        }
        return true;
    }

    public function relations() {
        return array(
            'GoodsModel' => array(self::BELONGS_TO, 'GoodsModel', 'goods_id'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    //记录分析结果
    public function saveFenxi($data) {
        if (empty($data['goods_id'])) {
            return false;
        }
        $count = $this->count($this->where(array('goods_id' => $data['goods_id'], 'years' => $data['years'], 'month' => $data['month'], 'day' => $data['day'])));
        if ($count) {
            $info = $this->find($this->where(array('goods_id' => $data['goods_id'], 'years' => $data['years'], 'month' => $data['month'], 'day' => $data['day'])));
            $info->attributes = $data;
            $status = $info->save();
        } else {
            unset($this->id);
            //自定义主键内容
            $data['keys'] = md5($data['goods_id'] . $data['years'] . $data['month'] . $data['day']);
            $this->attributes = $data;
            $this->setIsNewRecord(true);
            $status = $this->save();
        }
        return $status;
//        $key = md5("saveFenxi_" . $data['goods_id'] . $data['years'] . $data['month'] . $data['day']);
//        $isNews = RedisCluster::getInstance()->get($key);
//        if ($isNews) {
//            $info = $this->find($this->where(array('goods_id' => $data['goods_id'], 'years' => $data['years'], 'month' => $data['month'], 'day' => $data['day'])));
//            if (empty($info)) {
//                return false;
//            }
//            $info->attributes = $data;
//            return $info->save();
//        } else {
//            $endtime = strtotime("{$data['years']}-{$data['month']}-{$data['day']} 23:59:59");
//            $time = time() - $endtime;
//            if ($time) {
//                RedisCluster::getInstance()->set($key, 1, $time);
//            }
//            unset($this->id);
//            $this->attributes = $data;
//            $this->setIsNewRecord(true);
//            return $this->save();
//        }
    }

    /**
     * 根据报名主键ID获取对应审核人员UID
     * @param type $goodsbm_id
     * @return int
     */
    public function getAdminId($goodsbm_id) {
        if (empty($goodsbm_id)) {
            return 0;
        }
        $model = GoodsBmModel::model();
        $info = $model->findByPk($goodsbm_id);
        if (empty($info)) {
            return 0;
        }
        return $info->admin_id;
    }

}
